package com.tangyuan.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.tangyuan.common.result.Result;
import com.tangyuan.common.result.ResultCodeEnum;
import com.tangyuan.common.util.DateUtil;
import com.tangyuan.dto.UserReportDto;
import com.tangyuan.feignapis.headlines.IHeadlinesFeignClient;
import com.tangyuan.feignapis.live.ILiveRoomFeignClient;
import com.tangyuan.feignapis.video.IVideoFeignClient;
import com.tangyuan.model.Headlines;
import com.tangyuan.model.UserReport;
import com.tangyuan.model.Video;
import com.tangyuan.model.live.LiveRoom;
import com.tangyuan.user.mapper.UserReportMapper;
import com.tangyuan.user.service.UserReportService;
import com.tangyuan.vo.UserReportVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserReportServiceImpl implements UserReportService {

    @Autowired
    UserReportMapper userReportMapper;

    @Autowired
    ILiveRoomFeignClient liveRoomFeignClient;

    @Autowired
    private IVideoFeignClient videoFeignClient;

    @Autowired
    private IHeadlinesFeignClient headlinesFeignClient;
    @Override
    public Result userReport(Long userId, UserReportDto userReportDto) {
        UserReport userReport = BeanUtil.copyProperties(userReportDto, UserReport.class);
        userReport.setUserId(userId);
        userReport.setCreateTime(DateUtil.changeLocalDateTimeToDate(LocalDateTime.now()));

        Long reportedUserId = 0L;

        switch (userReportDto.getType()){
            case 0:
                //用户举报
                reportedUserId = userReportDto.getReportId();
                break;
            case 1:
                //视频举报
                Video video = videoFeignClient.getByVideoId(userReportDto.getReportId());
                if(video != null){
                    reportedUserId = video.getUserId();
                }
                break;
            case 2:
                //文章举报
                Headlines headlines = headlinesFeignClient.getByHeadlinesId(userId,userReportDto.getReportId());
                if(headlines != null){
                    reportedUserId = headlines.getUserId();
                }
                break;
            case 3:
                // 投票详情中作品举报
                reportedUserId = userReportMapper.getUserIdByHonouredGuestId(userReportDto.getReportId());
                break;
            case 4:
                // 直播举报
                LiveRoom liveRoom = liveRoomFeignClient.getLiveById(userReportDto.getReportId());
                if(liveRoom != null){
                    reportedUserId = liveRoom.getUserId();
                }
                break;
            default:
                reportedUserId = null;
        }
        if(reportedUserId == null || reportedUserId == 0){
            return Result.build(null, ResultCodeEnum.NO_DATA);
        }
        userReport.setReportedUserId(reportedUserId);
        userReportMapper.insert(userReport);
        return Result.ok();

    }

    @Override
    public Result getReport(Long id) {
        List<UserReportVo> list = userReportMapper.getReport(id);
        List<UserReportVo> resultList=new ArrayList<>();
        for (UserReportVo userReportVo : list) {
            SimpleDateFormat sdfLogin = new SimpleDateFormat("yyyy-MM-dd");
            Date remindTime = userReportVo.getRemindTime();
            Timestamp currentTime = new Timestamp(System.currentTimeMillis());
            String formatCurrentTime = sdfLogin.format(currentTime);
            String formatRemindTime =null;
            if (remindTime !=null){
                 formatRemindTime = sdfLogin.format(remindTime);
            }
            if (remindTime==null ||!formatRemindTime.equals(formatCurrentTime)){
                userReportMapper.setRemindTime(id);
               if (userReportVo.getType() == 0) {
                   userReportVo.setName(userReportMapper.getUserNameById(userReportVo.getReportId()));
                   userReportVo.setRemindTime(new Date());
               }
               // 视频的举报
               if (userReportVo.getType() == 1) {
                   Video video = videoFeignClient.getByVideoId(userReportVo.getReportId());
                   userReportVo.setName(video.getTitle());
                   userReportVo.setRemindTime(new Date());
               }
               if (userReportVo.getType() == 2) {
                   Headlines headlines = headlinesFeignClient.getByHeadlinesId(id,userReportVo.getReportId());
                   userReportVo.setName(headlines.getTitle());
                   userReportVo.setRemindTime(new Date());
               }
               if (userReportVo.getType() == 4) {
                   LiveRoom liveRoom = liveRoomFeignClient.getLiveById(userReportVo.getReportId());
                   userReportVo.setName(liveRoom.getRoomName());
                   userReportVo.setRemindTime(new Date());
               }
               resultList.add(userReportVo);
           }

        }
        return Result.ok(resultList);
    }
}
